/**
 * Created by yuhongdong on 16/6/6.
 */

function showPic(whichpic){

    if (!document.getElementById("placeholder")) return false;
    var source = whichpic.getAttribute("href");
    var placeholder = document.getElementById("placeholder");
    if (placeholder.nodeName != "IMG") return false;
    placeholder.setAttribute("src",source);
    if (document.getElementById("description")){
        var text = whichpic.getAttribute("title") ? whichpic.getAttribute("title") : "";
        var description = document.getElementById("description");
        if (description.firstChild.nodeType == 3){
            description.firstChild.nodeValue = text;
        }
    }
    return true;
}

function popUp(winURL){
    window.open(winURL,"popup","width = 320,height = 480");
}

function prepareLinks(){
    if (!document.getElementsByTagName) return false;
    var links = document.getElementsByTagName("a");

    if (links.length>0){
        for (var i = 0;i<links.length;i++){

            if (links[i].getAttribute("class")=="popup"){

                links[i].onclick = function(){
                    popUp(this.getAttribute("href"));
                    return false;
                }
            }

        }
    }
}


function prepareGallery(){

    if (!document.getElementsByTagName) return false;
    if (!document.getElementById) return false;
    if (!document.getElementById("imagegallery")) return false;

    var  gallery = document.getElementById("imagegallery");
    var links = gallery.getElementsByTagName("a");
    for (var i = 0 ; i<links.length; i++){
        links[i].onclick = function(){
            return showPic(this)?false:true;
        }
    }


}


function insertAfter(newElement,targetElement){
    var parent = targetElement.parentNode;
    if (parent.lastChild == targetElement){
        parent.appendChild(newElement);
    }else {
        parent.insertBefore(newElement,targetElement.nextElementSibling);
    }
}

function preparePlaceholder(){

    if (!document.createElement) return false;
    if (!document.createTextNode) return false;
    if (!document.getElementById) return false;
    if (!document.getElementById("imagegallery")) return false;

    var placeholder = document.createElement("img");
    placeholder.setAttribute("id","placeholder");
    placeholder.setAttribute("src","images/placeholder.jpg");
    placeholder.setAttribute("alt","my image gallery");

    var description = document.createElement("p");
    description.setAttribute("id","description");
    var text = document.createTextNode("Choose an image");
    description.appendChild(text);

    //document.getElementsByTagName("body")[0].appendChild(placeholder);
    //document.getElementsByTagName("body")[0].appendChild(description);

    //var gallery = document.getElementById("imagegallery");
    //gallery.parentNode.insertBefore(description,gallery);
    //gallery.parentNode.insertBefore(placeholder,gallery);

    var gallery = document.getElementById("imagegallery");
    insertAfter(placeholder,gallery);
    insertAfter(description,placeholder);

}




function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function' ){
        window.onload = func;
    }else {
        window.onload = function(){
            oldonload();
            func();
        }
    }
}


addLoadEvent(preparePlaceholder);
addLoadEvent(prepareLinks);
addLoadEvent(prepareGallery);

//window.onload = prepareLinks();
